#include "mydialog.h"
#include "ui_mydialog.h"
#include <QtCore>
#include <QEvent>

// inital values for sliders
int leftS = 250;
int rightS = 250;
bool correcting = false;
bool onePlayer = true;


/**********************
 * Constructor
 */
MyDialog::MyDialog(QWidget *parent, user *currentuser) :
    QDialog(parent),
    ui(new Ui::MyDialog)
{
    personal = currentuser;
    ui->setupUi(this);    
    ui->pauseButton->setEnabled(false);
    ui->stopButton->setEnabled(false);
    ui->horizontalSlider->setValue(personal->getGame());
    ui->LeftSlider->setValue(personal->getLeft());
    ui->RightSlider->setValue(personal->getRight());
    ui->scoreCheckBox->setChecked(personal->getShore());
    ui->highLCD->display(personal->getHigh());
}
// end construtor

void MyDialog::scoreChange(int whichone, int score){
        if(whichone == 1) ui->highLCD->display(score);
        else if (whichone == 0) ui->currLCD->display(score);
        else if(whichone == -1) ui->highLCD->display(score);
}


/**********************
 * Destructor
 */
MyDialog::~MyDialog()
{
    emit dyingCall();
    delete ui;
}
// end ~MyDialog()


/**********************
 * If 'x' button is hit, handle closure. Singleton destruction call.
 */
void MyDialog::closeEvent(QCloseEvent *event)
{
    emit dyingCall();
    delete ui;
    accept();
}


/**********************
 * Returns left slider value
 */
int MyDialog::getLeft()
{
    return leftS;
}
// end getLeft()


/**********************
 * Returns right slider value
 */
int MyDialog::getRight()
{
    return rightS;
}
// end getRight()


/**********************
 * Slot for left slider
 */
void MyDialog::on_LeftSlider_valueChanged(int value)
{
    personal->setLeft(value);
    leftS = value;
}
// end on_LeftSlider_valueChanged(int value)


/**********************
 * Slot for right slider
 */
void MyDialog::on_RightSlider_valueChanged(int value)
{
    personal->setRight(value);
    rightS = value;
}
// end on_RightSlider_valueChanged(int value)


/**********************
 * Time slider
 */
void MyDialog::on_horizontalSlider_valueChanged(int value)
{
    personal->setGame(value);
    emit setGameTime(value);
}


/**********************
 * Start button
 */
void MyDialog::on_startButton_clicked()
{
    if(!correcting) emit buttonPushed(1);
    ui->stopButton->setEnabled(true);
    ui->startButton->setEnabled(false);
    ui->pauseButton->setEnabled(true);
    ui->horizontalSlider->setEnabled(false);
}


/**********************
 * Pause button
 */
void MyDialog::on_pauseButton_clicked()
{
    if(!correcting) emit  buttonPushed(2);
    ui->startButton->setEnabled(true);
    ui->pauseButton->setEnabled(false);
}


/**********************
 * Stop button
 */
void MyDialog::on_stopButton_clicked()
{
   if(!correcting) emit  buttonPushed(0);
    ui->stopButton->setEnabled(false);
    ui->startButton->setEnabled(true);
    ui->pauseButton->setEnabled(false);
    ui->horizontalSlider->setEnabled(true);
}

/**********************
 * Slot for button changes in main window, update button in this dialog
 */
void MyDialog::fromTheOtherSide(int yup){
    correcting = true;
    if(yup == 0 || yup == 3) on_stopButton_clicked();
    else if (yup == 1) on_startButton_clicked();
    else if(yup == 2) on_pauseButton_clicked();
    correcting = false;
}


/**********************
 * Show scores chekcbox
 */
void MyDialog::on_scoreCheckBox_toggled(bool checked)
{
    personal->setShore(checked);
       emit showScore(checked);
}


/**********************
 * 1/2 player changes
 */
void MyDialog::isOnePlayer(bool yes){
    if(!yes){
        ui->label_9->setText("Player 1");
        ui->label_10->setText("Player 2");
        ui->highLCD->display(0);
        ui->currLCD->display(0);
    } else {
        ui->label_9->setText("High score");
        ui->label_10->setText("Current score");
        ui->highLCD->display(personal->getHigh());
        ui->currLCD->display(0);
    }
}
